package com.css.fxfzypg.modules.model.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.logInfo.SysLog;
import com.css.fxfzypg.modules.model.entity.DataPbeLosses;
import com.css.fxfzypg.modules.model.entity.EconomicLosses;
import com.css.fxfzypg.modules.model.service.EconomicLossesService;
import com.css.fxfzypg.modules.model.vo.CalculateVO;
import com.css.fxfzypg.modules.model.vo.EconomicVO;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2022年10月06日  15:12
 * 经济损失计算模型
 */
@RestController
@Validated
@RequestMapping("/economic")
public class EconomicLossesController {

    @Resource
    private EconomicLossesService economicLossesService;
    /***
     * 经济损失计算模型列表分页查询
     * @param vo
     */
    @SysLog("经济损失计算模型列表分页查询")
    @GetMapping("/page")
    public RestResponse getPage(EconomicVO vo, HttpServletRequest request){
        int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=economicLossesService.getPage(vo,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page,"查询成功!");
    }

    /***
     * 经济损失计算模型根据模型id查询信息
     * @param id
     */
    @GetMapping("/getModelById")
    public RestResponse getModelById(@NotBlank(message="模型id不能为空") @RequestParam("id") String id){
        EconomicLosses model = economicLossesService.getModelById(id);
        return ResponseCreator.createSuccessResponse(model);
    }

    /***
     *  经济损失计算模型id查询信息用于复制信息回显
     * @param id
     */
    @GetMapping("/getModelByIdForCopy")
    public RestResponse getModelByIdForCopy(@NotBlank(message="模型id不能为空") @RequestParam("id") String id){
        EconomicLosses model = economicLossesService.getModelByIdForCopy(id);
        return ResponseCreator.createSuccessResponse(model);
    }

    /***
     * 经济损失计算模型-用于复制保存
     * @param economicLosses
     */
    @PostMapping("/save")
    public RestResponse saveIntensityAttenuation(@Valid @RequestBody EconomicLosses economicLosses){
        economicLossesService.save(economicLosses);
        return ResponseCreator.createSuccessResponse("","保存成功");
    }

    /***
     * 经济损失计算模型-修改
     * @param economicLosses
     */
    @PostMapping("/update")
    public RestResponse update(@Valid @RequestBody EconomicLosses economicLosses){
        economicLossesService.update(economicLosses);
        return ResponseCreator.createSuccessResponse("","保存成功");
    }

    /***
     * 经济损失计算模型-删除
     * @param vo
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody EconomicVO vo){
        economicLossesService.delete(vo.getId());
        return ResponseCreator.createSuccessResponse("","删除成功");
    }

    /***
     * 经济损失计算
     * @param vo
     */
    @PostMapping("/calculate")
    public RestResponse calculate(@RequestBody CalculateVO vo){
        try {
            return economicLossesService.calculate(vo.getTaskId(),vo.getRange());
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.fail(503,e.getMessage());
        }
    }

    /***
     * 计算格网下的经济损失
     * @param vo
     */
    @PostMapping("/savePbeLosses")
    public RestResponse savePbeLosses(@RequestBody CalculateVO vo) {
        try {
            return economicLossesService.savePbeLosses(vo.getModeId(),vo.getTaskId(),vo.getTab(),vo.getRange());
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.fail(503,e.getMessage());
        }
    }

    /**
     * 导出各烈度下经济损失
     * @param
     * @return
     */
    @GetMapping("/exportPbeLosses")
    public void exportPbeInjuries(String taskId, HttpServletResponse resp){
        try {
            //  return casualtyService.countIntensityAndStructureOfArea((String) map.get("taskId"),(String) map.get("tab"),(String) map.get("range"));
            economicLossesService.exportPbeInjuries(taskId,resp);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出各烈度下经济损失数据失败");
        }
    }

    /**
     * @author Dongbj
     * @date 2022/11/28 11:01
     * @desc  评估结果经济损失分布图
     */
    @GetMapping("getPbeLossesByTaskIdAndIntensity")
    public RestResponse getPbeByTaskIdAndIntensity(@NotBlank(message = "任务id不能为空") @RequestParam String taskId,
                                                   @NotBlank(message = "地震烈度不能为空") @RequestParam String dzIntensity){
        List<DataPbeLosses> pbeList = economicLossesService.getPbeByTaskIdAndIntensity(taskId,dzIntensity);
        return ResponseCreator.createSuccessResponse(pbeList);
    }
}
